<?php

namespace App\Events;
use App\Model\Business\Classe;
use App\Model\Business\Schedule;
use App\Model\Eeoapi\Eeocourse;
use App\Model\Eeoapi\Eeoschedule;
use App\Model\Foundation\Student;
use App\Model\System\Eeofile;

class CourseEvent extends Event
{
    #### chimp
    # 班级类型
    public $type ;
    # Chimp 课程ID
    private $chimpCourseId ;
    # classuid
    public $classuid;

    #### eeo 需要数据
    # eeo 云盘目录ID
    public  $folderId;
    # eeo 课程封面
    public  $Filedata;
    # eeo 课程ID
    public  $courseId;
    # eeo 课程名称
    public  $courseName;
    # eeo 课程过期时间
    public  $expiryTime;
    # eeo 直播/回放地址
    public  $webcast;

    /**
     * 学生数据
     * @var array
     * @refer setStudent
     */
    public  $identity;
    public  $students;
    public  $oldStudents;


    /**
     * 课节数据
     * @var array
     * @refer setLesson
     */
    public  $lessons;



    /**
     * CourseEvent constructor.
     * @param $action
     * @param $classe
     */
    public function __construct(Classe $classe, $action=null)
    {

        $this->lessons = [];

        $this->setIdentity();
        $this->students = [];
        $this->oldStudents = [];
        if(!empty($classe->cover)){
            $photo = $classe->cover;
        }else{
            $photo = Event::getOrg('defaultCourseImage');
        }
        if(!empty($photo)){
            $photo = str_replace('/api/file/', '', $photo);
            $photo = str_replace('/api/file', '', $photo);
            $photo = str_replace('?u=', '', $photo);
            $photo = Eeofile::getLocalPath($photo);
            if(file_exists($photo))
                $this->Filedata = $photo;
        }

        $this->action = $action;
        $this->type = Schedule::LESSON;

        $this->classuid = $classe->classuid;
        $this->chimpCourseId = $classe->classuid;
        $this->courseName = $classe->coursename;

        if(!is_null($classe->folderid)){
            $this->folderId = $classe->folderid;
        }
        # 回查
        if($action <> Event::CREATE){
            $eeocourse = Eeocourse::where('type', $this->type)
                        ->where('chimpid', $this->chimpCourseId)
                        ->first();
            if($eeocourse){
                $this->courseId = $eeocourse->eeocourseid;
            }
        }
    }

    public function getClasseId()
    {
        return $this->classuid;
    }

    /**
     * 设置EEO 返回的课程id
     * @param $id
     */
    public function setEeoCourseId($id)
    {
        $this->courseId = $id;
        $fileds = [
            'type'=>$this->type,
            'chimpid'=>$this->chimpCourseId,
            'eeocourseid'=>$this->courseId,
        ];
        Eeocourse::firstOrCreate($fileds);
    }

    public function setIdentity($code=1)
    {
        $this->identity = $code;
    }

    public function delEeoCourseId()
    {
        Eeocourse::where('chimpid',$this->chimpCourseId)
            ->where('type',$this->type)
            ->delete();
        $lessons = Schedule::where('classeid', $this->chimpCourseId)->get();
        foreach($lessons as $lesson){
            Eeoschedule::where('scheduleid',$lesson->scheduleid)
                ->delete();
        }
    }

    public function getIdentity()
    {
        return $this->identity;
    }
    /**
     * 设置EEO 返回的课程直播回放地址
     * @param $data
     */
    public function setWebcast($data)
    {
        if(!empty($data)){
            $classe = Classe::find($this->classuid);
            $classe->webcast = $data;
            $classe->save();
        }
    }

    /**
     * 返回学生信息
     * @return array
     */
    public function getStudents($old=false)
    {
        if($old){
            return $this->oldStudents;
        }
        return $this->students;
    }

    /**
     * 附加学生
     * @param $student
     */
    public function setStudent($student, $old=false)
    {
        if($old){
            $id = $student->studentuid;
            if(!key_exists($id, $this->students)){
                $this->oldStudents[$id]['identity'] = $this->getIdentity();
                $this->oldStudents[$id]['studentName'] = $student->name;
                $this->oldStudents[$id]['studentAccount'] = $student->ClassInNo;
            }
        }else{
            $id = $student->studentuid;
            if(!key_exists($id, $this->students)){
                $this->students[$id]['identity'] = $this->getIdentity();
                $this->students[$id]['studentName'] = $student->name;
                $this->students[$id]['studentAccount'] = $student->ClassInNo;
            }
        }
    }

    /**
     * 批量添加学生
     * @param Student $students
     */
    public function setStudents($students, $old=false)
    {
        foreach($students as $student){
            $this->setStudent($student, $old);
        }
    }

    /**
     * 获取课程
     * @return mixed
     */
    public function getLessons()
    {
        return $this->lessons;
    }

    /**
     * 添加一个课程
     * @param Schedule $schedule
     */
    public function setLesson(Schedule $schedule, $action)
    {
        $this->lessons[] = new ScheduleEvent($schedule, $action);
    }

    /**
     *  批量添加课程
     * @param Schedule $schedules
     */
    public function setLessons(Schedule $schedules, $action)
    {
        foreach($schedules as $schedule){
            $this->setLesson($schedule, $action);
        }
    }

    /**
     * 记录
     */
    public function __destruct()
    {


    }
}
